import { constantRoutes } from '@/router'
import Vue from 'vue'
import Router from 'vue-router'
import Layout from '@/layout'
import { MessageBox, Message } from 'element-ui'
import { asyncAdminRoutes } from '@/router'
Vue.use(Router)
    /* Layout */
    /**
     * Use meta.role to determine if the current user has permission
     * @param roles
     * @param route
     */
function hasPermission(roles, route) {
    if (route.meta && route.meta.roles) {
        return roles.some(role => route.meta.roles.includes(role))
    } else {
        return true
    }
}
function filterAsyncRouter(asyncRouterMap) { //遍历后台传来的路由字符串，转换为组件对象
    const accessedRouters = asyncRouterMap.filter(route => {
      //这里因为所有的路由组件都为Layout所以这里直接去判断路由中的component是否为Layout
        if (route.component) {
            if (route.component == 'Layout') { //Layout组件特殊处理
                route.component = Layout
            } else {
                const component = route.component
                route.component = resolve => require(['@/views' + component], resolve)
            }
        }
        if (route.children && route.children.length > 0) {
            route.children = filterAsyncRouter(route.children)
        }
        return true
    })
    return accessedRouters
}
const state = {
        routes: [],
        addRoutes: [],
        roleName: "",
        defaultPath: "/",
        redirect:'',
        adminOrUser:false,
    }
    //修改状态值mutations 方法
const mutations = {
        setPath: (state, newVal) => {
            state.defaultPath = newVal
        },
  changeAdmin:(state,)=>{
  },
        setRedirect: (state, newVal) => {
          state.redirect = newVal
        },
        setRoleName: (state, name) => {
            state.roleName = name;
        },
        SET_ROUTES: (state, routes) => {
            state.addRoutes = routes
            state.routes = constantRoutes.concat(routes)
        },
        changeRoutes: (state, result) => {
            state.routes.map(o => {
                if (o.path.indexOf(result) != -1) {
                    o.hidden = false
                } else {
                    o.hidden = true
                }
            })
        },
    }
    //去判断角色的权限
const actions = {
    generateRoutes({ commit }, roles) {
        return new Promise(resolve => {
            let accessedRoutes
            if (roles=='1'){

                accessedRoutes = asyncAdminRoutes||[]
                commit('SET_ROUTES', accessedRoutes)
                resolve(accessedRoutes)
            } else {
              /**
               * 判断每个角色进来的菜单
               */
              accessedRoutes=asyncAdminRoutes||[]
              commit('SET_ROUTES', accessedRoutes)
              resolve(accessedRoutes)
            }
        })
    },
    getRoleName() {
        return new Promise(resolve => {
            resolve(state.roleName);
        })

    },
    changeRedirect({ commit }, redirect) {
      return new Promise((resolve, reject) => {
        commit('setRedirect',redirect)
      })
    },
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
